<%--
  Created by IntelliJ IDEA.
  User: Tim-Lee
  Date: 2018/6/9
  Time: 13:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件下载Demo</title>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('a').click(function () {
                var fileName = $(this).attr("fileName");
                $.ajax({
                    url: '${pageContext.request.contextPath}/checkFileExists',//验证文件是否存在
                    type: 'post',
                    data: {fileName: fileName},
                    dataType: 'json',
                    success: function (data) {
                        if (data.success) {
                            //文件存在开始下载
                            /*                           $("#myForm").attr("action","/"+data.url)
                                                       $("#myForm").submit();*/
                            location.href = '${pageContext.request.contextPath}/' + data.url;//文件存在时，直接请求下载链接，文件名已经保存至session
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function () {

                    }
                });
            });
        });

    </script>
</head>
<body>

<div>
    <p>
        <a href="javascript:;" download="我的文件" fileName="美女.jpg">a标签下载文件</a>
    </p>
    <form action="" method="POST" id="myForm"></form>
</div>
</body>
</html>
